package com.example.eventbus

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import com.hil.eventbus.Subscriber
import com.hil.eventbus.ThreadMode

/**

https://blog.csdn.net/weixin_34032779/article/details/87975944

https://github.com/werbhelius/EventBusKotlin

 * */
class MainActivity : AppCompatActivity() {
    val TAG :String= MainActivity::javaClass.name
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.btn_main).setOnClickListener {
            EventBus.post(MainEvent())
        }


        findViewById<Button>(R.id.btn_async).setOnClickListener {
            EventBus.post(PostEvent())
        }



    }

    @Subscriber(mode = ThreadMode.MAIN)
    fun mainReciver(mainEvent : MainEvent){
        Log.e(TAG,"mainReciver currentThread = "+Thread.currentThread().id)
    }
    @Subscriber(mode = ThreadMode.BACKGROUND)
    fun asyncReciver(asyncEvent : PostEvent){
        Log.e(TAG,"asyncReciver currentThread ="+Thread.currentThread().id)
    }

    override fun onStart() {
        super.onStart()
        EventBus.register(this)
    }


    override fun onStop() {
        super.onStop()
        EventBus.unRegister(this)

    }

    class PostEvent:IEvent

    class MainEvent:IEvent
}